package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("开始处理登录请求:{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam.getUsername());
        if (userVO==null){
            log.debug("用户名不存在");
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public int saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        user.setPassword("123456");
        user.setCreateTime(new Date());
        int rows = userMapper.insertUser(user);
        if (rows!=1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return 1;
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("开始处理查询用户信息的请求");
        List<UserVO> userVOList = userMapper.selectUser(userQuery);
        return userVOList;
    }

    @Override
    public int editUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        user.setUpdateTime(new Date());
        int rows = userMapper.updateUser(user);
        if (rows!=1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return rows;
    }

    @Override
    public int resetPasswordSer(Long id) {
        User user = new User();
        user.setId(id);
        user.setPassword("root");
        if (userMapper.updateUser(user)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateStatus(Long id, String status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        if (userMapper.updateUser(user)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteUser(Long id) {
        if (userMapper.deleteUserByd(id)==1){
            return 1;
        }
        return 0;
    }

    @Override
    public List<UserVO> selectAuditUser(Long parentId) {
        List<UserVO> AuditList ;
        UserQuery userQuery = new UserQuery();
        userQuery.setId(parentId);
        AuditList=userMapper.selectUser(userQuery);
        if(AuditList.get(0).getParentId()!=null){
            userQuery.setId(AuditList.get(0).getParentId());
            AuditList.add(userMapper.selectUser(userQuery).get(0));
        }
        return AuditList;
    }
}
